package cn.appsys.controller;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.appsys.pojo.AppCategory;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.service.devUser.DevUserService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;

@Controller
public class DevAppinfoListController {
	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	DevUserService devUserService=(DevUserService) context.getBean("devUserService");
	
	@RequestMapping(value="/dev/categorylevellist.json")
	@ResponseBody
	public Object applevelist(@RequestParam(value="pid",required=false) String pid){
		Integer _pid=null;
		if(null != pid){
			_pid=Integer.parseInt(pid);
		}
		List<AppCategory> categoryLeve1List=devUserService.getCategoryleve(_pid);
		return  JSON.toJSONString(categoryLeve1List);
	}
	//分页查询列表
	@RequestMapping(value="/dev/appinfoList")
	public Object appinfoList(Model model,
		@RequestParam(value="querySoftwareName",required=false) String querySoftwareName,  //软件名称
		@RequestParam(value="queryStatus",required=false) String queryStatus,    //APP状态
		@RequestParam(value="queryFlatformId",required=false) String queryFlatformId,  //所属平台
		@RequestParam(value="queryCategoryLevel1",required=false) String queryCategoryLevel1, //一级分类
		@RequestParam(value="queryCategoryLevel2",required=false) String queryCategoryLevel2, //二级分类
		@RequestParam(value="queryCategoryLevel3",required=false) String queryCategoryLevel3,//三级分类
		@RequestParam(value="pageIndex",required=false) String pageIndex
		){
			//获得状态列表
			List<DataDictionary>  statusList=devUserService.getStatusList("APP_STATUS");
			model.addAttribute("statusList",statusList);
			//获得所属平台列表
			List<DataDictionary> flatFormList=devUserService.getStatusList("APP_FLATFORM");
			model.addAttribute("flatFormList",flatFormList);
			//获得一级分类
			List<AppCategory> categoryLevel1List=devUserService.getCategoryleve(null);
			model.addAttribute("categoryLevel1List",categoryLevel1List);
			
			/*分页查询App信息列表*/
			if(null==querySoftwareName){
				querySoftwareName="";
			}
			Integer _queryStatus=null;
			if(null!=queryStatus && ""!=queryStatus){
				_queryStatus=Integer.valueOf(queryStatus);
			}
			Integer _queryFlatformId=null;
			if(null!=queryFlatformId && ""!=queryFlatformId){
				_queryFlatformId=Integer.valueOf(queryFlatformId);
			}
			Integer _queryCategoryLevel1=null;
			if(null!=queryCategoryLevel1 && ""!=queryCategoryLevel1){
				_queryCategoryLevel1=Integer.valueOf(queryCategoryLevel1);
			}
			Integer _queryCategoryLevel2=null;
			if(null!=queryCategoryLevel2 && ""!=queryCategoryLevel2){
				_queryCategoryLevel2=Integer.valueOf(queryCategoryLevel2);
			}
			Integer _queryCategoryLevel3=null;
			if(null!=queryCategoryLevel3 && ""!=queryCategoryLevel3){
				_queryCategoryLevel3=Integer.valueOf(queryCategoryLevel3);
			}
			
			//页面大小
			int pageSize = Constants.pageSize;
			//当前页码
	    	int currentPageNo = 1;
	    	if(pageIndex !=null && pageIndex !=""){
					currentPageNo=Integer.valueOf(pageIndex);
					
	    	}
	    	//总数量（表）
	    	int totalCount	= devUserService.getUserCount(querySoftwareName,_queryStatus,
	    						_queryFlatformId,_queryCategoryLevel1,
	    						_queryCategoryLevel2,_queryCategoryLevel3);
	    	
	    	//总页数
	    	PageSupport pages=new PageSupport();
	    	pages.setCurrentPageNo(currentPageNo);
	    	pages.setPageSize(pageSize);
	    	pages.setTotalCount(totalCount);
	    	int totalPageCount = pages.getTotalPageCount();
	    	
	    	//控制首尾页
	    	if(currentPageNo < 1){
	    		currentPageNo = 1;
	    	}else if(currentPageNo > totalPageCount){
	    		currentPageNo = totalPageCount;
	    	}
	    	List<AppInfo> appInfoList=devUserService.getAppInfoList(querySoftwareName,_queryStatus,
					_queryFlatformId,_queryCategoryLevel1,
					_queryCategoryLevel2,_queryCategoryLevel3,
					currentPageNo, pageSize);
	    	
	    	model.addAttribute("appInfoList",appInfoList);
	    	model.addAttribute("pages",pages);
			return "/developer/appinfolist";
			
		}

}
